define([
	'jquery',
	'underscore',
	'backbone',
	'catjs/util',
	'weibo',
	],function($,_,Backbone,util,weibo){

		var module = {};

		//住户视图
		var ResidentView = Backbone.View.extend({

			tagName : 'li',
			className : 'dormitory-li',
			template : $('#tplResident').text(),
			events : {
				'mouseenter .dormitory-photo' : 'showUserInfo',				
			},

			render : function(){
				var self = this;
				self.$el.html( util.ejs.render(self.template,self.model) );
				return self.$el;
			},
			//显示用户信息
			showUserInfo : function(e){
				var self = this;
				weibo.citizenCard.model = self.model;
				weibo.citizenCard.render(e.pageX,e.pageY);
			},
		});
		//宿舍楼视图
		var DormBuildingView = Backbone.View.extend({

			el : $('#dorm-building'),
			events : {
				//离开宿舍
				'click .dormitory-exit' : 'leave',
			},

			init : function(){
				var self = this;
				self.$el.find('.dormitory-ul').empty();
			},
			render : function(dormId){
				var self = this;
				self.init();
				//获取数据
				$.ajax({
					url : '/Node/GetResident',
					type : 'GET',
					data : {
						dormId : dormId,
					},
					success : function(json){
						var users = JSON.parse(json);
						//渲染
						users.forEach(function(user){
							var resident = new ResidentView;	
							resident.model = user;
							self.$el.find('.dormitory-ul').append( resident.render() );
						});
						$('#dorm-building').fadeIn();
					},
				});
			},
			//离开宿舍
			leave : function(){
				$('#dorm-building').fadeOut();
			},
		});
		var dormBuilding = new DormBuildingView;



		module.enter = function(dormId){
			console.log(dormId);
			dormBuilding.render(dormId);
		};
		return module;
});